package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart save(Cart cartParam) {
        //查询当前菜品/套餐在数据表中是否存在（当前登录用户user_id）
        //创建条件
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartLambdaQueryWrapper.eq(
                cartParam.getSetmealId() != null,
                Cart::getSetmealId,
                cartParam.getSetmealId());
        cartLambdaQueryWrapper.eq(
                cartParam.getDishId() != null,
                Cart::getDishId,
                cartParam.getDishId());
        Cart cart = cartMapper.selectOne(cartLambdaQueryWrapper);
        if (cart != null) {
            //查到当前菜品/套餐在数据库的记录， 只需要将number + 1 更新
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);

            return cart;
        }else {
            //没有查到当前菜品/套餐的记录， 新增记录
            cartParam.setUserId(UserHolder.get().getId());//这只登录用户的id
            cartParam.setNumber(1);//设置为1
            cartParam.setCreateTime(new Date());//保存时间
            cartMapper.insert(cartParam);

            return cartParam;
        }
    }

    @Override
    public List<Cart> findList() {
        //这只查询条件
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartLambdaQueryWrapper.orderByAsc(Cart::getCreateTime);
        List<Cart> carts = cartMapper.selectList(cartLambdaQueryWrapper);
        //执行查询
        return carts;
    }

    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(cartLambdaQueryWrapper);
    }

    @Override
    public Cart updateCart(String dishId, String setmealId) {
        //创建条件dishId
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq( Cart::getUserId, UserHolder.get().getId());
        cartLambdaQueryWrapper.eq(dishId != null,Cart::getDishId, dishId);
        cartLambdaQueryWrapper.eq(setmealId != null, Cart::getSetmealId, setmealId);

        //执行查询
        Cart cart = cartMapper.selectOne(cartLambdaQueryWrapper);

        //判断
        cart.setNumber(cart.getNumber() - 1);
        cartMapper.updateById(cart);
        if (cart.getNumber() <= 0) {
            cartMapper.deleteById(cart);
        }
        return cart;
    }
}
